#ifndef CONSOLE_LOG_BUF_H
#define CONSOLE_LOG_BUF_H

#include <iostream>
#include <algorithm>
#include "log_message_pool.h"

class ConsoleLogBuf : public std::streambuf 
{
public:
	ConsoleLogBuf()
	{
		LogMessageBlock * pLogMsg = m_logMsgPool.require_message_block();
		setp(LogMessageBlockAccess::get_buffer(*pLogMsg).begin(), LogMessageBlockAccess::get_buffer(*pLogMsg).end());		
	}

	virtual ~ConsoleLogBuf()
	{
	}

	int sync ()
	{		
		std::copy(pbase(),pptr(),std::ostream_iterator<char>(std::cout));
		//std::cout <<pbase();
		*(pptr()) = 0;
		LogMessageBlock * pLogMsg = m_logMsgPool.require_message_block();
		setp(LogMessageBlockAccess::get_buffer(*pLogMsg).begin(), LogMessageBlockAccess::get_buffer(*pLogMsg).end());		
		return 0; 
	}

	int overflow(int c)
	{
		sync();
		if (c != EOF) 
		{
			*pptr() = static_cast<unsigned char>(c);
			pbump(1);
		}
		return 0;
	}

	// Defining xsputn is an optional optimization.
	// (streamsize was recently added to ANSI C++, not portable yet.)
	// 	std::streamsize xsputn(const char *_Ptr, std::streamsize _Count)
	// 	{
	// 		return _Count;
	// 	}

private:	
	LogMessagePool m_logMsgPool;
};


#endif // 
